﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project.MVC.DataAccess
{
    public class NewAccess
    {
        Linq2SQLDataContext dataContext;
        public NewAccess()
        {
            dataContext = new Linq2SQLDataContext();
        }
        public IQueryable<New> GetList()
        {
            return dataContext.News.OrderByDescending(x=>x.Id);
        }
        public void Edit(New _object)
        {
            var objectDB = dataContext.News.Where(c => c.Id == _object.Id).SingleOrDefault();
            if (objectDB != null)
            {
                objectDB.Title = _object.Title;
                objectDB.ShortDescription = _object.ShortDescription;
                objectDB.Description = _object.Description;
                objectDB.Image = _object.Image;
                objectDB.Type = _object.Type;
            }
            dataContext.SubmitChanges();
        }
        public New GetByKey(int id)
        {
            return dataContext.News.Where(c => c.Id == id).SingleOrDefault();
        }
        public void Insert(New _object)
        {
            dataContext.News.InsertOnSubmit(_object);
            dataContext.SubmitChanges();
        }
        public void Delete(New _object)
        {
            var objectDB = dataContext.News.Where(c => c.Id == _object.Id).SingleOrDefault();
            if (objectDB != null)
            {
                dataContext.News.DeleteOnSubmit(objectDB);
                dataContext.SubmitChanges();
            }
        }
    }
}
